package cn.i96688.scanclient;

import javax.swing.JOptionPane;
import javax.swing.UIManager;

import com.alibaba.fastjson.JSONObject;
import cn.i96688.scanclient.bean.PayLog;
import cn.i96688.scanclient.bean.Status;
import cn.i96688.scanclient.service.MerchantService;
import cn.i96688.scanclient.service.impl.MerchantServiceImpl;
import cn.i96688.scanclient.ui.MainForm;
import cn.i96688.scanclient.ui.LoginForm;
import cn.i96688.scanclient.utils.AppEnv;
import cn.i96688.scanclient.utils.ConfParams;
import cn.i96688.scanclient.utils.LocalStore;
import cn.i96688.scanclient.utils.PayUtil;

public class Main {
	public static void main(String[] args) {
		try {
			if(AppEnv.appOnExist()){
				PayLog.getInstance().addLog("已经启动一个程序，不能再次启动");
				return;
			}
//			AppEnv.startServer();
			start();
		} catch (java.lang.Throwable e) {
			PayLog.getInstance().addLogs(e);
			JOptionPane.showConfirmDialog(null, e.getLocalizedMessage(), "启动出错", JOptionPane.ERROR_MESSAGE);
		}
		
	}
	
	private static void start(){
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				try{
					init();
					//弹出登录和参数设置框
					LoginForm paramForm=new LoginForm(
							new LoginForm.SaveListener() {
								@Override
								public void save(boolean status) {
									if(status){
										MainForm mainForm=new MainForm();
										mainForm.showForm();//成功，启动主界面
//										mainForm.setExtendedState(MainForm.ICONIFIED | mainForm.getExtendedState());//自动缩小
//										hide(mainForm);
									}else{
										JOptionPane.showMessageDialog(null, "必须配置正确的参数", "操作错误", JOptionPane.ERROR_MESSAGE);
									}
								}
							});
					//商户数据没有登录并初始化
					if(LocalStore.getInstance().getMerInf().size()==0){
						
						paramForm.showForm();
					}else{
						MerchantService paramService=new MerchantServiceImpl();
						//每次启动程序时要获取商户数据
						JSONObject merInf=paramService.getMerInf();
						//商户数据获取失败，重新登录获取
						if(Status.isFail(merInf)){
							paramForm.showForm();
//							JOptionPane.showMessageDialog(null, merInf.optString(Status.NAME_MSG), "数据初始化错误", JOptionPane.ERROR_MESSAGE);
						}else{
							//成功，启动主界面
							MainForm mainForm=new MainForm();
							mainForm.showForm();//成功，启动主界面
//							mainForm.setExtendedState(MainForm.ICONIFIED | mainForm.getExtendedState());
//							hide(mainForm);
						}
					}
				}catch(Throwable e){
					PayLog.getInstance().addLogs(e);
					JOptionPane.showMessageDialog(null,e.getLocalizedMessage(), "启动出错", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
	}
	private static void hide(final MainForm form){
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				form.setExtendedState(MainForm.ICONIFIED | form.getExtendedState());
			}
		});
	}
	private static void init() throws Exception{
		ConfParams.readPropertiesConf();
		UIManager.put("swing.boldMetal", Boolean.FALSE);
		System.setProperty("https.protocols","TLSv1");
		try {
			ConfParams.setValueByKey("deviceName", PayUtil.getLocalMAC());
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null,e.getLocalizedMessage(), "获取IP出错", JOptionPane.ERROR_MESSAGE);
		}
	}
}
